package Server;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import org.codehaus.jettison.json.JSONObject;

import Server.BusinessObjects.Highscore;
import Server.BusinessObjects.Question;

@Path("/answer")
public class AnswerResource {

	@GET
	@Produces(MediaType.APPLICATION_JSON)
	@Path("/{questionId}/{selectedoption}")
	public String commitAnswer(@PathParam("questionId") int questionId,
			@PathParam("selectedoption") int selectedoption) {

		JSONObject o = new JSONObject();

		try {
			System.out.println(String.format("Get Answer# %s Answer %s",
					questionId, selectedoption));

			Question q = Question.getQuestion(questionId);
			
			if(q.getRightOptionNumber() == selectedoption) {
				
				
				o.put("richtig",
						"1");
			} else {
				o.put("falsch",
						"0");
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}

		return o.toString();
	}
	
	@GET
	@Produces(MediaType.APPLICATION_JSON)
	@Path("/{questionId}")
	public String commitAnswer(@PathParam("questionId") int questionId) {

		JSONObject o = new JSONObject();

		try {
			Question q = Question.getQuestion(questionId);
			
			o.put("Id", q.getId());
			o.put("Name", q.getName());
			o.put("Option1", q.getOption1());
			o.put("Option2", q.getOption2());
			o.put("Option3", q.getOption3());
			o.put("Option4", q.getOption4());
			o.put("RightOptionNumber", q.getRightOptionNumber());
			
			System.out.println(String.format("Get Question# %s",
					questionId));
			
		} catch (Exception e) {
			e.printStackTrace();
		}

		return o.toString();
	}
	
	@GET
	@Produces(MediaType.APPLICATION_JSON)
	@Path("/{userid}/{scenId}/{catId}/{questionId}/{selectedoption}")
	public String commitAnswer(@PathParam("userid") int userId,
			@PathParam("scenId") int scenId,
			@PathParam("catId") int catId,
			@PathParam("questionId") int questionId,
			@PathParam("selectedoption") int selectedoption) {

		JSONObject o = new JSONObject();

		try {
			Question q = Question.getQuestion(questionId);
			
			if(q.getRightOptionNumber() == selectedoption) {
				Highscore.insertHighscore(userId, scenId, catId, q.getSeverity());		
				o.put("richtig",
						"1");
			} else {
				Highscore.insertHighscore(userId, scenId, catId, -1);
				o.put("falsch",
						"0");
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}

		return o.toString();
	}
}